package com.wtwd.campus.utils;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.WxPushVo;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jeewx.api.core.req.model.message.TemplateData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import sun.net.www.http.HttpClient;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author pxin
 */
@Component
public class WXPushUtils {
    private static final Logger logger = LoggerFactory.getLogger(WXPushUtils.class);

    private static String appId = "wxb70939670a55edb7";
    private static String secret = "44865d98a5b43332e26e1ab9a1b56115";
    // private String templateId = "IzxhFnuSFyZCrHbAWWuHxGusZIRlROzcXX3QpK069DE";

    /**
     * 点击模块消息要跳转到的地址
     */
    private String url = "";

    /**
     * 微信推送
     *
     * @param data
     */
    public void wxPush(WxPushVo data) {
        logger.info("推送微信消息到：data={} " + data.toString());
        //先配置模板
        Integer type = data.getType();
        String templateId = "";
        if (type == 1) {
            //进围栏
            templateId = "IzxhFnuSFyZCrHbAWWuHxGusZIRlROzcXX3QpK069DE";
        } else if (type == 2) {
            //出围栏
            templateId = "IzxhFnuSFyZCrHbAWWuHxGusZIRlROzcXX3QpK069DE";
        }
        //配置
        WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpInMemoryConfigStorage.setAppId(appId);
        wxMpInMemoryConfigStorage.setSecret(secret);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpInMemoryConfigStorage);
        //构建推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(data.getOpenId())
                .templateId(templateId)
                .url(url)
                .build();
        //设置消息参数的具体值
        templateMessage = type == 1 ? getInFenceWxTemplateMessage(templateMessage) : getOutFenceWxTemplateMessage(templateMessage);
        try {
            //推送消息
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (WxErrorException e) {
            logger.info("推送失败到用户: openId={}" + data.getOpenId());
            e.printStackTrace();
        }
    }

    /**
     * 配置进围栏的模板（仅作范例内容可更改）
     *
     * @return
     */
    public WxMpTemplateMessage getInFenceWxTemplateMessage(WxMpTemplateMessage templateMessage) {
      /*  templateMessage.addData(new WxMpTemplateData(("first"), "尊敬的家长，您的小孩超出围栏。","#173177"));
        templateMessage.addData(new WxMpTemplateData(("keyword1"),"高三二班","#FF0000"));
        templateMessage.addData(new WxMpTemplateData(("keyword2"), "张三","#FF0000"));
        templateMessage.addData(new WxMpTemplateData(("keyword3"), "600米","#FF0000"));
        templateMessage.addData(new WxMpTemplateData(("remark"), "为了保证安全，请及时联系！","#173177"));
        return templateMessage;*/
        return templateMessage;
    }

    /**
     * 配置出围栏的模板（仅做范例内容可更改）
     *
     * @return
     */
    public WxMpTemplateMessage getOutFenceWxTemplateMessage(WxMpTemplateMessage templateMessage) {
      /*  templateMessage.addData(new WxMpTemplateData(("first"), "尊敬的家长，您的小孩超出围栏。","#173177"));
        templateMessage.addData(new WxMpTemplateData(("keyword1"),data.getClassName(),"#FF0000"));
        templateMessage.addData(new WxMpTemplateData(("keyword2"), data.,"#FF0000"));
        templateMessage.addData(new WxMpTemplateData(("keyword3"), "600米","#FF0000"));
        templateMessage.addData(new WxMpTemplateData(("remark"), "为了保证安全，请及时联系！","#173177"));
        return templateMessage;*/
        return templateMessage;
    }


}
